import React, { FC, useEffect, useState } from 'react'
import { Form, Input, Row, Col, Button, message } from 'antd'
import { detailProFile, updateProFile } from '@/services/proFile'
import ProCard from '@ant-design/pro-card'

const FormItem = Form.Item
const formTailLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 15 }
}

interface ControllerConfigFormProps {
  fileId:string,
}

const ControllerConfigForm: FC<ControllerConfigFormProps> = (props) => {

  const [form] = Form.useForm()
  const { fileId } = props

  const [fileDetail, setFileDetail] = useState<any>({})
  useEffect(() => {
    detailProFile({ id: fileId }).then(res => {
      if (res.code === 10000) {
        setFileDetail(res.data)
        form.resetFields();
      }
    })
  },[fileId])

  const handleOk = async () => {
    const { validateFields } = form
    const values = await validateFields()
    values.id = fileId
    // 提交数据
    updateProFile(values).then(res => {
      if (res.code === 10000) {
        message.success('保存成功！')
      }
    })
  }


  return (
    <ProCard style={{paddingLeft:24,paddingTop:24}} title='Controller配置'>
      <Form form={form} initialValues={fileDetail} onFinish={() => handleOk()}>
        <Row>
          <Col md={12}>
            <FormItem {...formTailLayout} name="comment" label="类注释">
              <Input placeholder="请输入类注释"/>
            </FormItem>
            <FormItem {...formTailLayout} name="req_path" rules={[{ required: true, message: '请输入请求地址' }]} label="请求地址">
              <Input placeholder="请输入请求地址"/>
            </FormItem>
            <FormItem {...formTailLayout} name="name" rules={[{ required: true, message: '请选择是否泛型' }]} label="类名">
              <Input placeholder="请输入类名"/>
            </FormItem>
          </Col>
          <Col md={12}>
            <FormItem>
              <Button type="primary" htmlType="submit" style={{ marginLeft: 300, marginTop: 57 }}>保存</Button>
            </FormItem>
          </Col>
        </Row>
      </Form>
    </ProCard>
  )
}

export default ControllerConfigForm
